
/*------------------------------------
	load playlist by filename
------------------------------------*/
function MuseX.LoadPlaylist( fname )

	// make sure it has the .txt extension
	if ( fname:sub( #fname - 3 ) != ".txt" ) then
	
		// if not, add it
		fname = ("%s.txt"):format( fname );
		
	end

	// build the full filename
	local fullfile = ("musex/playlists/%s"):format( fname );
	
	// check that it exists
	if ( !file.Exists( fullfile ) ) then
	
		// if it doesn't, don't continue
		return;
		
	end
	
	// read the file data
	local data = file.Read( fullfile ) or "";
	
	// get each song by explode data per line
	local songs = string.Explode( "\n", data );
	
	// empty songlist table
	local songlist = {};
	
	// loop through songs
	for _, song in pairs( songs ) do
	
		// make sure the file exists and is valid
		if ( song && MXUtility.PathExists( song ) && #song > 4 ) then
		
			// add to songlist table
			songlist[ #songlist + 1 ] = song;
		
		end
	
	end
	
	// empty the playlist
	MuseX.ClearPlaylist();
	
	// add the loaded songs
	MuseX.AddToPlaylist( songlist );

end


/*------------------------------------
	save a playlist to a filename
------------------------------------*/
function MuseX.SavePlaylist( fname, songs )

	// make sure it has the .txt extension
	if ( fname:sub( #fname - 3 ) != ".txt" ) then
	
		// if not, add it
		fname = ("%s.txt"):format( fname );
		
	end

	// empty data
	local data = "";

	// loop through each song
	for _, song in pairs( songs ) do
	
		// build the data string
		data = data .. song .. "\n";
	
	end
	
	// build the full filename
	local fullfile = ("musex/playlists/%s"):format( fname );
	
	// write the data to file
	file.Write( fullfile, data );

end


/*------------------------------------
	update now playing playlist
------------------------------------*/
function MuseX.UpdateNowPlaying( )

	// save the current playlist
	MuseX.SavePlaylist( "nowplaying", MuseX.GetPlaylist() );

end
